package com.ys.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.extend.common.model.AjaxJson;
import com.ys.extend.utils.CacheKeyGenerator;
import com.ys.extend.utils.ContextHolderUtils;
import com.ys.web.cache.QueryCache;
import com.ys.web.pojo.AccountInfo;
import com.ys.web.remotemodel.RemoteAccountInfo;
import com.ys.web.service.AccountService;

@Scope("prototype")
@Controller
@RequestMapping("/accountController")
public class AccountController {

	@Autowired
	private AccountService accountService;
	
	/**
	 * 不用了
	 * @param companyId
	 * @return

	@RequestMapping(params="loadCompanyAccount")
	@ResponseBody
	public List<AccountInfo> getAccountByCompany(Integer companyId){
		 //System.out.println("companyId:"+companyId);
		 List<AccountInfo> accountInfos = new ArrayList<AccountInfo>();
		 if(companyId==null){
			 //System.out.println("return");
			 return accountInfos;
		 }
		
		
		
		 Integer userId= ContextHolderUtils.getSessionUserId();
		 
		try {
			
			String query_cache_key=  CacheKeyGenerator.generateQueryCacheKey(userId,AccountInfo.class.getName(),List.class.getName(),companyId.toString());
			accountInfos = (List<AccountInfo>) QueryCache.getQueryObject(query_cache_key);//通过缓存获取账户列表数据
			//缓存没数据则直接从数据库获取
			if(accountInfos==null){
				accountInfos = accountService.getAccountByCompanyId(companyId);
			}
			//JSONArray json = JSONArray.fromObject(accountInfos);
			//System.out.println(json.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return accountInfos;
	}	 */

	/**
	 * 根据公司查找账号
	 * @param companyId
	 * @return
	 */
	@RequestMapping(params="loadCompanyAccount")
	@ResponseBody
	public List<RemoteAccountInfo> getAccountByCompany(Integer companyId){
		List<RemoteAccountInfo> list = new ArrayList<>();
		try {
			list = accountService.getAccountByCompanyKADId(companyId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 临时存放账户
	 * @param accountInfo
	 */
	@RequestMapping(params="saveTempAccount")
	@ResponseBody
	public AjaxJson saveTempAccountForCompany(@RequestBody RemoteAccountInfo accountInfo){
		AjaxJson ajaxJson = new AjaxJson();
		Integer userId= ContextHolderUtils.getSessionUserId();
		Integer companyId = accountInfo.getCompanyId();
		
		String query_cache_key=  CacheKeyGenerator.generateQueryCacheKey(userId,AccountInfo.class.getName(),List.class.getName(),companyId.toString());
		
		List<RemoteAccountInfo> accountInfoTemps = null;
		if(QueryCache.getQueryObject(query_cache_key)==null){
			try {
				accountInfoTemps = accountService.getAccountByCompanyKADId(companyId);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				ajaxJson.setMsg("操作异常");
				ajaxJson.setSuccess(false);
				return ajaxJson;
			}
			
				
		}else{
			accountInfoTemps=(List<RemoteAccountInfo>) QueryCache.getQueryObject(query_cache_key);
		}
		
		if(accountInfoTemps==null){
			accountInfoTemps = new ArrayList<RemoteAccountInfo>();
			
			if(!accountService.isAccountExist(accountInfo)){
				
				accountInfoTemps.add(accountInfo);
			}else{
				ajaxJson.setMsg("账户名已存在");
				ajaxJson.setSuccess(false);
				return ajaxJson;
			}
			
			
		}
				QueryCache.updateQueryObject(query_cache_key, accountInfoTemps);
				accountInfoTemps=(List<RemoteAccountInfo>) QueryCache.getQueryObject(query_cache_key);
				//System.out.println("accountInfoTemps"+accountInfoTemps);
				//判断是否用户是否存在在缓存或系统中
				if(accountInfo.getId()==null){
					if(!accountService.isAccountExist(accountInfo)){
						accountInfoTemps.add(accountInfo);
					}else{
						ajaxJson.setMsg("账户名已存在");
						ajaxJson.setSuccess(false);
						return ajaxJson;
					}
					
				}else{
					for(int i=0;i<accountInfoTemps.size();i++){
						if(null!=accountInfo.getId()){
							if(accountInfoTemps.get(i).getId().equals(accountInfo.getId())){
								if(!accountService.isHasSameAccount(accountInfo)){
									accountInfoTemps.set(i, accountInfo);
								}else{
									ajaxJson.setMsg("账户名重复");
									ajaxJson.setSuccess(false);
									return ajaxJson;
								}
								
							}
						}
						
					}
				}
				
				QueryCache.updateQueryObject(query_cache_key, accountInfoTemps);
				ajaxJson.setMsg("账户名成功");
				ajaxJson.setSuccess(true);
				return ajaxJson;
				
		
	}
	
	@RequestMapping(params="deleteTempAccount")
	@ResponseBody
	public String removeTempAccount(Integer id,Integer companyId){
		Integer userId= ContextHolderUtils.getSessionUserId();
		//System.out.println("removeCompanyId:"+companyId);
		String msg = "删除成功";
		//System.out.println("request companyId:"+companyId);
		String query_cache_key=  CacheKeyGenerator.generateQueryCacheKey(userId,AccountInfo.class.getName(),List.class.getName(),companyId.toString());
		List<RemoteAccountInfo> accountInfoTemps = null;
		if(QueryCache.getQueryObject(query_cache_key)==null){
			try {
				accountInfoTemps = accountService.getAccountByCompanyKADId(companyId);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				msg = "删除异常";
				return msg;
			}
			
				
		}else{
			accountInfoTemps=(List<RemoteAccountInfo>) QueryCache.getQueryObject(query_cache_key);
		}
		
		if(accountInfoTemps==null||accountInfoTemps.size()==0){
			msg="没有信息删除";
		}
		
		for(int i=0;i<accountInfoTemps.size();i++){
			if(null!=id){
				if(id.equals(accountInfoTemps.get(i).getId())){
					//System.out.println("removeId:"+id);
					accountInfoTemps.remove(i);
				}
			}
			
		}
			
			QueryCache.updateQueryObject(query_cache_key, accountInfoTemps);
		
		
		
			return msg;
		
	}
	

	/**
	 * 更新账号
	 * @param accountInfo
	 * @return
	 */
	@RequestMapping(params="updateAccount")
	@ResponseBody
	public AjaxJson updateAccount(@RequestBody RemoteAccountInfo remoteAccountInfo) {
		AjaxJson j = new AjaxJson();
		try {
			accountService.updateRemoteAccountInfo(remoteAccountInfo);			
		} 
		catch (Exception e) {
			j.setSuccess(false);
			e.printStackTrace();
		}
		
		return j;
	}	
	
	
	
}
